package com.dayuanit.booksystem.v3.service.impl;

import com.dayuanit.booksystem.v3.domain.PublisherInfo;
import com.dayuanit.booksystem.v3.entity.Book;
import com.dayuanit.booksystem.v3.entity.Publisher;
import com.dayuanit.booksystem.v3.exception.BizException;
import com.dayuanit.booksystem.v3.mapper.BookMapper;
import com.dayuanit.booksystem.v3.mapper.PublisherMapper;
import com.dayuanit.booksystem.v3.service.PublisherService;
import com.dayuanit.booksystem.v3.util.MybatisUtil;
import com.dayuanit.booksystem.v3.util.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class PublisherServiceImpl implements PublisherService {

    @Override
    public void addPublisher(String name, String address) {
        if (StringUtils.isBlank(name) || StringUtils.isBlank(address)) {
            throw new BizException("有必填参数为空");
        }

        //sqlSession不是线程安全的，每次使用，都必须重新获取。
        //不能将sqlSession放到成员变量中，任意产生线程安全的问题。
        //SqlSession用完后，要将其关闭
        try (SqlSession sqlSession = MybatisUtil.getSqlSession();) {
            PublisherMapper mapper = sqlSession.getMapper(PublisherMapper.class);

            Publisher publisher = new Publisher();
            publisher.setAddress(address);
            publisher.setName(name);
            int rows = mapper.insert(publisher);
            if (1 != rows) {
                System.out.println("操作失败");
            }
        }
    }

    @Override
    public Publisher queryPublisher(String publisherName) {
        if (StringUtils.isBlank(publisherName)) {
            throw new BizException("请输入查询参数");
        }

        try (SqlSession sqlSession = MybatisUtil.getSqlSession();) {
            PublisherMapper mapper = sqlSession.getMapper(PublisherMapper.class);
            Publisher publisher = mapper.getPublisherByName(publisherName);
            return publisher;
        }
    }

    @Override
    public Publisher queryPublisher(long publisherId) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession();) {
            PublisherMapper mapper = sqlSession.getMapper(PublisherMapper.class);
            Publisher publisher = mapper.getPublisherById(publisherId);
            return publisher;
        }
    }

    @Override
    public void updatePublisherAddress(long publisherId, String address) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession();) {
            PublisherMapper mapper = sqlSession.getMapper(PublisherMapper.class);
            int rows = mapper.updateAddress(publisherId, address);
            if (1 != rows) {
                System.out.println("更新失败");
            }
        }
    }

    @Override
    public void addPublisherAndBook(String publisherName, String publisherAddress, String bookName, String author, String price) {
        if (StringUtils.isBlank(publisherName) || StringUtils.isBlank(publisherAddress)
        || StringUtils.isBlank(bookName) || StringUtils.isBlank(author)) {
            throw new BizException("有必填参数为空");
        }


        SqlSession sqlSession  = MybatisUtil.getSqlSession(false);
        try {
            // 先添加出版社
            PublisherMapper publisherMapper = sqlSession.getMapper(PublisherMapper.class);

            Publisher publisher = new Publisher();
            publisher.setAddress(publisherAddress);
            publisher.setName(publisherName);

            publisherMapper.insert(publisher);

            // 再添加图书
            BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
            bookMapper.insert(price, author, bookName, publisher.getId());

            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
        } finally {
            if (sqlSession != null) {
                sqlSession.close();
            }
        }
    }

    //方案一：
    //根据出版社ID查询出版社名称和地址，以及出版社下对应的所有图书的ID、名字、作者、价格
    @Override
    public PublisherInfo loadPublisherInfo(long publisherId) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
            //TODO 获取出版社信息
            PublisherMapper publisherMapper = sqlSession.getMapper(PublisherMapper.class);
            Publisher publisher = publisherMapper.getPublisherById(publisherId);

            //TODO 在获取当前出版社下的图书
            BookMapper bookMapper = sqlSession.getMapper(BookMapper.class);
            List<Book> books = bookMapper.listBook(publisher.getId());

            //将以上出版社和图书的信息 封装到对象里，返回之
            PublisherInfo publisherInfo = new PublisherInfo();
            publisherInfo.setBooks(books);
            publisherInfo.setPublisherAddress(publisher.getAddress());
            publisherInfo.setPublisherName(publisher.getName());
            return publisherInfo;
        }
    }


    //根据出版社ID查询出版社名称和地址，以及出版社下对应的所有图书的ID、名字、作者、价格
    //方案二：
    @Override
    public PublisherInfo loadPublisherInfo2(long publisherId) {
        try (SqlSession sqlSession = MybatisUtil.getSqlSession()) {
            PublisherMapper publisherMapper = sqlSession.getMapper(PublisherMapper.class);
            PublisherInfo publisherInfo = publisherMapper.getPublisherInfo(publisherId);
            return publisherInfo;
        }
    }
}
